<?php
namespace Admin\Controller;
use Think\Controller;
class ProductController extends AdminController {
	//产品展示
    public function index(){
        $product = M('product');
        if(I('sort')){
            $data['parentid'] = I('sort');
            $this->assign('sort1',I('sort'));
        }
        if(I('start') && I('end')){
            $start = strtotime(I('start'));
            $end = strtotime(I('end'))+86399;
            $data['time'] = array(array('elt',$end),array('egt',$start));
        }
        if(I('product')){
            $data['product'] = I('product');
        }
        $count = $product->where($data)->count();// 查询满足要求的总记录数
        $Page  = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $this->product = $product->where($data)->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('count',$count);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->sort=M('sort')->select();
        $this->display();
    }
    //添加产品页面
    public function add_product(){
        $this->sort=M('sort')->select();
    	$this->display();
    }
    //添加产品
    public function addend_product(){
        $data['product']=I('product');
        $data['parentid']=I('parentid');
        $data['content']=I('content');
        $data['beizhu']=I('beizhu');
        $data['image']=I('image');
        $data['time']=time();
        if(M('product')->add($data)){
            echo 1;
        }else{
            echo 2;
        }
    }
    //添加产品类别页面
    public function add_sort_page(){
        $this->display();
    }
    //添加产品
    public function add_sort(){
        $data['sort']=I('sort');
        $data['time']=time();
        if(M('sort')->add($data)){
            echo 1;
        }else{
            echo 2;
        }
    }
    //删除产品
    public function del_product(){
        $id = I('id');
        $p = M('product')->where("id='$id'")->find();
        $path = '.'.$p['image'];
        if(unlink($path)){
            if(M('product')->where("id='$id'")->delete()){
                $this->ajaxReturn(V(1,'已删除'));
            }else{
                $this->ajaxReturn(V(3,'图片文件已删除，删除数据时出错'));
            }
        }else{
            $this->ajaxReturn(V(2,'删除数据失败，图片原文件未删除'));
        }
    }
    //编辑信息页面
    public function save_product_page(){
        $id = I('id');
        $this->sort=M('sort')->select();
        $p = M('product')->where("id='$id'")->find();
        $path = '.'.$p['image'];
        if(!file_exists($path)){
            $p['image']='/Public/weizhi.png';
        }
        $this->assign('p',$p);
        $this->assign('id1',$id);
        $this->display();
    }
    //编辑信息
    public function save_product(){
        $id = I('id');
        $data['product']=I('product');
        $data['parentid']=I('parentid');
        $data['content']=I('content');
        $data['beizhu']=I('beizhu');
        $data['image']=I('image');
        $data['time']=time();
        if(M('product')->where("id='$id'")->save($data)){
            $this->ajaxReturn(V(1,'修改成功'));
        }else{
            $this->ajaxReturn(V(2,'修改失败'));
        }
    }
    //查看标题
    public function title(){
        $this->sort=M('sort')->select();
        $this->display();
    }
    //删除标题
    public function del_title(){
        $id = I('id');
        if(M('product')->where("parentid='$id'")->find()){
            $this->ajaxReturn(V(0,'请先清除标题下的产品再删除标题'));
        }
        if(M('sort')->where("id='$id'")->delete()){
            $this->ajaxReturn(V(1,'删除成功'));
        }else{
            $this->ajaxReturn(V(0,'删除失败'));
        }
    }
}